package com.skmc.okcashbag.setting;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;

import com.skmc.okcashbag.home.R;

public class NoticeActivity extends Activity {

	private Button noticeBtn1;
	private Button noticeBtn2;
	private Button noticeBtn3;
	private Button noticeBtn4;
	private Button noticeBtn5;
	private Button closeBtn;
	private Button prevBtn;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	
	    setContentView(R.layout.setting_notice);
	    
	    noticeBtn1 = (Button)findViewById(R.id.noticeBtn1);
	    noticeBtn2 = (Button)findViewById(R.id.noticeBtn2);
	    noticeBtn3 = (Button)findViewById(R.id.noticeBtn3);
	    noticeBtn4 = (Button)findViewById(R.id.noticeBtn4);
	    noticeBtn5 = (Button)findViewById(R.id.noticeBtn5);

	    closeBtn = (Button)findViewById(R.id.closeBtn);
	    closeBtn.setOnClickListener(onClose);
	    
	    prevBtn = (Button)findViewById(R.id.prevBtn);
	    prevBtn.setOnClickListener(onPrev);
	    
	    setList();
	}
	private View.OnClickListener onClose = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			setResult( RESULT_OK, new Intent() );
			finish();
    		overridePendingTransition(R.anim.fade, R.anim.hold);
		}
 	};
 	
 	private View.OnClickListener onPrev = new View.OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			finish();
			overridePendingTransition(R.anim.fade, R.anim.hold);
			  			
		}
	};

	
	private void setList(){
		if(NoticeList.size() > 0){
			int cnt=0;
			if(NoticeList.size() < 5) cnt = NoticeList.size();
			else cnt= 5;
			noticeBtn1.setVisibility(View.INVISIBLE);
			noticeBtn2.setVisibility(View.INVISIBLE);
			noticeBtn3.setVisibility(View.INVISIBLE);
			noticeBtn4.setVisibility(View.INVISIBLE);
			noticeBtn5.setVisibility(View.INVISIBLE);
			
			for(int i = 1; i <= cnt; i++){
				Button noticeBtn = null;// = noticeBtn1;
				if(i==1) {
					noticeBtn = noticeBtn1;
					noticeBtn1.setVisibility(View.VISIBLE);
				}else if(i==2){
					noticeBtn = noticeBtn2;
					noticeBtn2.setVisibility(View.VISIBLE);
				}else if(i==3){
					noticeBtn = noticeBtn3;
					noticeBtn3.setVisibility(View.VISIBLE);
				}else if(i==4){ 
					noticeBtn = noticeBtn4;
					noticeBtn4.setVisibility(View.VISIBLE);
				}else if(i==5){
					noticeBtn = noticeBtn5;
					noticeBtn5.setVisibility(View.VISIBLE);
				}
				
				noticeBtn.setText(NoticeList.get(i-1).title);
				final String notice_seq = NoticeList.get(i-1).notice_seq;
				noticeBtn.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						Intent intent = new Intent( NoticeActivity.this, NoticeDetailActivity.class);
	        			intent.putExtra("notice_seq",notice_seq); 
	        			startActivityForResult( intent, 0 );
	        			overridePendingTransition(R.anim.fade, R.anim.hold);
						
					}
				});
				final Button Btn = noticeBtn;
				Btn.setOnTouchListener(new OnTouchListener() {
					
					@Override
					public boolean onTouch(View v, MotionEvent event) {
						switch(event.getAction()){
			            case MotionEvent.ACTION_DOWN:
			            	Btn.setBackgroundResource(R.drawable.setting_bg_list_over);
			                break;
			            case MotionEvent.ACTION_UP:
			            	Btn.setBackgroundResource(R.drawable.setting_bg_list01);
			                break;
			            }
						return false;
					}
				});


			}
			
		}
	}
	
	@Override
    public void onConfigurationChanged(Configuration newConfig) {
               // TODO Auto-generated method stub
               super.onConfigurationChanged(newConfig);
    }
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event){
		if(keyCode == KeyEvent.KEYCODE_BACK){
			finish();
			overridePendingTransition(R.anim.fade, R.anim.hold);
		}return super.onKeyDown(keyCode, event);
	}
	
	@Override
    protected void onActivityResult( int requestCode, int resultCode, Intent data ) {
        super.onActivityResult( requestCode, resultCode, data );
        
        if ( resultCode == RESULT_OK ) {
            
        	finish();
        	overridePendingTransition(R.anim.fade, R.anim.hold);
        }
    }

}
